package com.xywy.tangandroid.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.xywy.tangandroid.R;
import com.xywy.tangandroid.base.BaseActivity;
import com.xywy.tangandroid.bean.BaseHttpResponse;
import com.xywy.tangandroid.util.Constant;
import com.xywy.tangandroid.util.LogUtil;
import com.xywy.tangandroid.util.UIUtil;
import com.xywy.tangandroid.vollery.GetRequest;
import com.xywy.tangandroid.vollery.OnVolleyResponseListener;

/*
 * 更换手机号
 */
public class ChangePhoneNumberAt extends BaseActivity {
	private RelativeLayout left_img;
	private TextView middle_title;
	private RelativeLayout validation_phone_button;
	private EditText phone_number;
	private TextView phone_text;

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_change_phone_number);
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText(getResources().getString(
				R.string.mine_account_validation_phonen_str));
		validation_phone_button = (RelativeLayout) findViewById(R.id.validation_phone_button);
		phone_text = (TextView) findViewById(R.id.phone_text);
		phone_number = (EditText) findViewById(R.id.phone_number);
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
		validation_phone_button.setOnClickListener(this);
	}

	@Override
	protected void initData() {
		phone_text.setText(getIntent().getStringExtra("phone_number"));

	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;
		case R.id.validation_phone_button:
			if (isMobile(phone_number.getText().toString().trim())) {
				sendValidationCode();
			} else {
				Toast.makeText(context, "请输入正确的手机号！", Toast.LENGTH_SHORT)
						.show();
			}
			break;
		default:
			break;
		}
	}

	public static boolean isMobile(String phone) {
		// TODO Auto-generated method stub
		PhoneNumberUtils.isGlobalPhoneNumber(phone);
		return PhoneNumberUtils.isGlobalPhoneNumber(phone);
	}

	/*
	 * 发送验证码
	 */
	private void sendValidationCode() {
		// TODO Auto-generated method stub
		final String target = phone_number.getText().toString().trim();
		GetRequest<BaseHttpResponse> getRequest = new GetRequest<BaseHttpResponse>(
				Constant.getUrl("sms", "sendCode") + "&target=" + target
						+ "&type=3", BaseHttpResponse.class,
				new OnVolleyResponseListener<BaseHttpResponse>() {

					@Override
					public void onError(VolleyError volleyError) {
						UIUtil.DissDialog(context, waittingDialog);
						UIUtil.showToast(context,
								getResources().getString(R.string.network_slow));
					}

					@Override
					public void onSuccess(BaseHttpResponse response) {
						UIUtil.DissDialog(context, waittingDialog);
						if (response.getCode() == 10000) {
							LogUtil.i(context, response.getId());
							Intent intent = new Intent(context,
									ComfrimCodeAct.class);
							intent.putExtra("flag", 1);
							intent.putExtra("target", target);
							intent.putExtra("id", response.getId());
							startActivity(intent);
							SharedPreferences sp = getSharedPreferences("xywy_tang_user",
									context.MODE_PRIVATE);		
							sp.edit().putString("phonenum", target).commit();
							ChangePhoneNumberAt.this.finish();
						} else {

							UIUtil.showToast(context, response.getError());
						}
					}
				});
		executeRequest(getRequest);

	}
}
